package com.tiezhu.springdemo.controller;



import com.tiezhu.springdemo.bean.FeedHistory;
import com.tiezhu.springdemo.bean.Product;
import com.tiezhu.springdemo.mapper.HistoryMapper;
import com.tiezhu.springdemo.mapper.ProductMapper;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.mapping.MappedStatement;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;


@RestController
public class ProductController {

    @Autowired
    private ProductMapper productMapper;
    @Autowired
    private HistoryMapper historyMapper;


    @GetMapping("queryProduct")
    @ResponseBody
    public Map<String,Object> queryProductByOwnerName(@RequestParam("owner_name")String owner_name){
        Map<String,Object> hashmap = new LinkedHashMap<>();
        List<Product> productList = productMapper.queryProductByOwnerName(owner_name);
        System.out.println(productList);
        if (productList.size() == 0){
            hashmap.put("status",1);
            hashmap.put("msg","产品不存在");
            return hashmap;
        }
        else {
            hashmap.put("status",0);
            hashmap.put("msg","产品存在");
            for (int i = 0; i < productList.size(); i++) {
                Product product = productList.get(i);
                hashmap.put("Product"+(i+1),product);
            }
            return hashmap;
        }
    }

    @PostMapping("/addProduct")
    public Map<String,Object> addProduct(@RequestBody Product product){
        Map<String,Object> hashMap = new LinkedHashMap<>();
        int i = productMapper.addProduct(product);
        if (i != 1){
            hashMap.put("status",1);
            hashMap.put("msg","添加失败");
            return hashMap;
        }
        hashMap.put("status",0);
        hashMap.put("msg","添加成功");
        return hashMap;
    }

    @GetMapping("/updateProduct")
    public Map<String,Object> updateProduct(@RequestBody Product product){
        Map<String,Object> hashMap = new LinkedHashMap<>();
        int i = productMapper.updateProduct(product);
        if (i != 1){
            hashMap.put("status",1);
            hashMap.put("msg","修改失败");
            return hashMap;
        }else {
            List<Product> productList = productMapper.queryProductByProductId(product.getProduct_id());
            hashMap.put("status",0);
            hashMap.put("msg","修改成功");
            hashMap.put("Product",productList.get(0));
            return hashMap;
        }
    }

    @PostMapping("/deleteProduct")
    public Map<String,Object> deleteProductByProductName(@RequestBody Product product){
        Map<String ,Object> hashMap= new LinkedHashMap<>();
        int i = productMapper.deleteProductByProductId(product);
        if (i != 1){
            hashMap.put("status",1);
            hashMap.put("msg","删除失败");
            return hashMap;
        }else {
            hashMap.put("status",0);
            hashMap.put("msg","删除成功");
            return hashMap;
        }
    }


    @GetMapping("/queryHistory")
    public Map<String,Object> queryHistoryByOwnerName(@RequestParam("owner_name")String owner_name){
        Map<String,Object> hashmap = new LinkedHashMap<>();
        List<FeedHistory> list = historyMapper.queryHistoryByOwnerName(owner_name);
        if (list.size()!=0){
            hashmap.put("status",1);
            hashmap.put("msg","查询成功");
            for (int i = 0; i < list.size(); i++) {
                hashmap.put("History",list);
            }
            return hashmap;
        }
        hashmap.put("status",1);
        hashmap.put("msg","没有数据");
        hashmap.put("History",null);
        return hashmap;


    }
}
